package com.share.common.utils;


import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class CookieUitl {
    public static String JSESSIONID = "JSESSIONID";

    public static String getValue(HttpServletRequest request, String key) {
        Cookie[] cookies = request.getCookies();
        String token = null;
        for (Cookie cookie : cookies) {
            if (key.equals(cookie.getName())) {
                token = cookie.getValue();
                break;
            }
        }
        return token;
    }

    public static String getToken(HttpServletRequest request) {
        return getValue(request, JSESSIONID);
    }

    public static final int COOKIE_MAX_AGE = 7 * 24 * 3600;
    public static final int COOKIE_HALF_HOUR = 30 * 60;

    /**
     * 根据Cookie名称得到Cookie对象，不存在该对象则返回Null
     *
     * @param rep
     * @param name
     * @return
     */
    public static Cookie getCookie(HttpServletRequest rep, String name) {
        Cookie[] cookies = rep.getCookies();
        if (ArrayUtils.isEmpty(cookies)) {
            return null;
        }
        Cookie cookie = null;
        for (Cookie c : cookies) {
            if (name.equals(c.getName())) {
                cookie = c;
                break;
            }
        }
        return cookie;
    }

    /**
     * 根据Cookie名称直接得到Cookie值
     *
     * @param rep
     * @param name
     * @return
     */
    public static String getCookieValue(HttpServletRequest rep, String name) {
        Cookie cookie = getCookie(rep, name);
        if (cookie != null) {
            return cookie.getValue();
        }
        return null;
    }

    /**
     * 移除cookie
     *
     * @param rep
     * @param rsp
     * @param name 这个是名称，不是值
     */
    public static void removeCookie(HttpServletRequest rep, HttpServletResponse rsp, String name) {
        if (null == name) {
            return;
        }
        Cookie cookie = getCookie(rep, name);
        if (null != cookie) {
            cookie.setPath("/");
            cookie.setValue("");
            cookie.setMaxAge(0);
            rsp.addCookie(cookie);
        }
    }

    /**
     * 添加一条新的Cookie，可以指定过期时间(单位：秒)
     *
     * @param rsp
     * @param name
     * @param value
     * @param maxValue
     */
    public static void setCookie(HttpServletResponse rsp, String name, String value, int maxValue) {
        if (StringUtils.isBlank(name)) {
            return;
        }
        if (null == value) {
            value = "";
        }
        Cookie cookie = new Cookie(name, value);
        cookie.setPath("/");
        if (maxValue != 0) {
            cookie.setMaxAge(maxValue);
        } else {
            cookie.setMaxAge(COOKIE_HALF_HOUR);
        }
        rsp.addCookie(cookie);
        try {
            rsp.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加一条新的Cookie，默认30分钟过期时间
     *
     * @param rsp
     * @param name
     * @param value
     */
    public static void setCookie(HttpServletResponse rsp, String name, String value) {
        setCookie(rsp, name, value, COOKIE_HALF_HOUR);
    }

    /**
     * 将cookie封装到Map里面
     *
     * @param rep
     * @return
     */
    public static Map<String, Cookie> getCookieMap(HttpServletRequest rep) {
        Map<String, Cookie> cookieMap = new HashMap<String, Cookie>();
        Cookie[] cookies = rep.getCookies();
        if (ArrayUtils.isNotEmpty(cookies)) {
            for (Cookie cookie : cookies) {
                cookieMap.put(cookie.getName(), cookie);
            }
        }
        return cookieMap;
    }
}
